package com.pac.weixin.util;

import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.common.tools.TimeTools;
import com.kuanrf.commons.weixin.api.WxKfService;
import com.kuanrf.commons.weixin.api.WxService;
import com.kuanrf.commons.weixin.api.help.WxReceiveEventHandler;
import com.kuanrf.commons.weixin.common.WxConsts;
import com.kuanrf.commons.weixin.exception.WxErrorException;
import com.kuanrf.commons.weixin.msg.bean.WxMsg;
import com.kuanrf.commons.weixin.msg.custom.bean.WxCustomMsg;
import com.kuanrf.commons.weixin.msg.reply.bean.BaseReplyMsg;
import com.pac.weixin.WeiXinKeyContent;
import com.pac.weixin.bean.WxUserBean;
import com.pac.weixin.service.WxUserService;

/**
 * 微信事件消息处理
 * 
 * @author lujun.chen
 * @version [版本号, 2015年4月5日]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@Component
public class WxReceiveEHandler implements WxReceiveEventHandler {
	
	private static Logger logger = Logger.getLogger(WxReceiveEHandler.class);

	@Autowired
	private WxUserService wxUserService;

	@Autowired
	private WxKfService wxKfService;

	/** 关注 */

	@Override
	public BaseReplyMsg attention(WxMsg wxMsg, Map<String, Object> context,
			WxService wxMpService) throws WxErrorException {

		// 检查微信用户是否已经关注
		WxUserBean wxUserBean = new WxUserBean();
		wxUserBean.setOpenid(wxMsg.getFromUserName());
		List<WxUserBean> wxUserList = wxUserService.queryWxUser(wxUserBean);
		wxUserBean.setSubscribe(WeiXinKeyContent.WEIXIN_SUBSCRIBE_YES);
		if (null == wxUserList || wxUserList.isEmpty()) {
			wxUserBean.setCreateDate(TimeTools.now());
			wxUserService.addWxUser(wxUserBean);
		} else {
			wxUserService.updateWxUser(wxUserBean);
		}

		// 向微信端发送消息
		try {
			String content = "尊敬的用户，您好！请您先选择医院，以获取问卷。";
			WxCustomMsg message = new WxCustomMsg();
			message.setMsgType(WxConsts.CUSTOM_MSG_TEXT);
			message.setContent(content);
			message.setToUser(wxMsg.getFromUserName());
			wxKfService.send(message);
		} catch (WxErrorException ex) {
			logger.error("向微信端发送消息出错，"+ex);
		} catch (Exception ex) {
			logger.error("向微信端发送消息出错，"+ex);
		}

		return null;
	}

	/** 取消关注 */

	@Override
	public BaseReplyMsg cancelAttention(WxMsg wxMsg,
			Map<String, Object> context, WxService wxMpService)
			throws WxErrorException {

		WxUserBean wxUserBean = new WxUserBean();
		wxUserBean.setOpenid(wxMsg.getFromUserName());
		wxUserBean.setUnsubscribeDate(TimeTools.now());
		wxUserBean.setSubscribe(WeiXinKeyContent.WEIXIN_SUBSCRIBE_NO);
		wxUserService.updateWxUser(wxUserBean);

		return null;
	}

	/** 扫描带参数二维码事件并关注 */

	@Override
	public BaseReplyMsg scanQrCodeAttention(WxMsg wxMsg,
			Map<String, Object> context, WxService wxMpService) {
		// TODO Auto-generated method stub
		return null;
	}

	/** 扫描带参数二维码事件但关注 */

	@Override
	public BaseReplyMsg scanQrCode(WxMsg wxMsg, Map<String, Object> context,
			WxService wxMpService) {
		// TODO Auto-generated method stub
		return null;
	}

	/** 上报地理位置事件 */

	@Override
	public BaseReplyMsg location(WxMsg wxMsg, Map<String, Object> context,
			WxService wxMpService) {
		// TODO Auto-generated method stub
		return null;
	}

	/** 点击菜单 */

	@Override
	public BaseReplyMsg clickMenuItem(WxMsg wxMsg, Map<String, Object> context,
			WxService wxMpService) {
		// TODO Auto-generated method stub
		return null;
	}

	/** 点击跳转链接 */

	@Override
	public BaseReplyMsg clickMenuItemLink(WxMsg wxMsg,
			Map<String, Object> context, WxService wxMpService) {
		// TODO Auto-generated method stub
		return null;
	}

	/** 点击跳转链接 */

	@Override
	public BaseReplyMsg kfCreateSession(WxMsg wxMsg,
			Map<String, Object> context, WxService wxMpService) {
		// TODO Auto-generated method stub
		return null;
	}

	/** 关闭会话 */

	@Override
	public BaseReplyMsg kfCloseSession(WxMsg wxMsg,
			Map<String, Object> context, WxService wxMpService) {
		// TODO Auto-generated method stub
		return null;
	}

	/** 关闭会话 */

	@Override
	public BaseReplyMsg kfSwitchSession(WxMsg wxMsg,
			Map<String, Object> context, WxService wxMpService) {
		// TODO Auto-generated method stub
		return null;
	}

	/** 事件推送群发结果 */

	@Override
	public BaseReplyMsg massSendJobFinish(WxMsg wxMsg,
			Map<String, Object> context, WxService wxMpService) {
		// TODO Auto-generated method stub
		return null;
	}

}
